# Unicode 修复脚本
# 修复 PowerShell 中的 Unicode 显示问题

Write-Host "🔧 Unicode 修复工具" -ForegroundColor Cyan
Write-Host "==================" -ForegroundColor White
Write-Host ""

# 步骤1：设置控制台编码
Write-Host "📝 步骤1: 设置控制台编码" -ForegroundColor Yellow
try {
    [Console]::OutputEncoding = [System.Text.Encoding]::UTF8
    [Console]::InputEncoding = [System.Text.Encoding]::UTF8
    Write-Host "✅ 控制台编码已设置为 UTF-8" -ForegroundColor Green
} catch {
    Write-Host "❌ 设置控制台编码失败" -ForegroundColor Red
}

Write-Host ""

# 步骤2：设置代码页
Write-Host "📝 步骤2: 设置系统代码页" -ForegroundColor Yellow
try {
    chcp 65001 | Out-Null
    Write-Host "✅ 代码页已设置为 65001 (UTF-8)" -ForegroundColor Green
} catch {
    Write-Host "⚠️  无法更改代码页，可能需要管理员权限" -ForegroundColor Yellow
}

Write-Host ""

# 步骤3：清除有问题的 PowerShell 配置
Write-Host "📝 步骤3: 检查并修复 PowerShell 配置" -ForegroundColor Yellow

$profilePath = $PROFILE.CurrentUserCurrentHost
if (Test-Path $profilePath) {
    Write-Host "🔍 发现现有 PowerShell 配置文件" -ForegroundColor Blue

    # 读取配置文件内容
    $content = Get-Content $profilePath -Raw -Encoding utf8

    # 检查是否有问题的 Unicode 字符
    if ($content -match '[\u{1F300}-\u{1F5FF}\u{1F600}-\u{1F64F}\u{1F680}-\u{1F6FF}]') {
        Write-Host "⚠️  检测到可能有问题的 Unicode 字符在配置文件中" -ForegroundColor Yellow

        # 创建备份
        $backupPath = "$profilePath.backup.$(Get-Date -Format 'yyyyMMdd_HHmmss')"
        Copy-Item $profilePath $backupPath -Force
        Write-Host "💾 已备份配置文件到: $backupPath" -ForegroundColor Blue

        # 创建简化的配置
        $simpleProfile = @'
# 简化的 PowerShell 配置 - UTF-8 支持

# 设置编码
[Console]::OutputEncoding = [System.Text.Encoding]::UTF8
[Console]::InputEncoding = [System.Text.Encoding]::UTF8
$PSDefaultParameterValues['Out-File:Encoding'] = 'utf8'

# 简单的提示符
function prompt {
    $path = $PWD.Path
    if ($path.Length -gt 40) {
        $path = "..." + $path.Substring($path.Length - 37)
    }
    "PS $path> "
}

Write-Host "PowerShell with Unicode support loaded." -ForegroundColor Green
'@

        $simpleProfile | Out-File -FilePath $profilePath -Encoding ascii -Force
        Write-Host "✅ 已创建简化的配置文件" -ForegroundColor Green
    } else {
        Write-Host "✅ PowerShell 配置文件看起来正常" -ForegroundColor Green
    }
} else {
    Write-Host "ℹ️  未找到 PowerShell 配置文件，将创建新配置" -ForegroundColor Blue
}

Write-Host ""

# 步骤4：测试 Unicode 输出
Write-Host "📝 步骤4: 测试 Unicode 支持" -ForegroundColor Yellow

$tests = @(
    @{"name" = "Basic ASCII"; "text" = "Hello World"},
    @{"name" = "Chinese"; "text" = "你好世界"},
    @{"name" = "Japanese"; "text" = "こんにちは"},
    @{"name" = "Korean"; "text" = "안녕하세요"},
    @{"name" = "Emoji"; "text" = "✅ 🎉 🚀"}
)

Write-Host ""
foreach ($test in $tests) {
    Write-Host "  Testing $($test.name): " -ForegroundColor Yellow -NoNewline
    try {
        Write-Host $test.text -ForegroundColor Green
    } catch {
        Write-Host "❌ FAILED" -ForegroundColor Red
    }
}

Write-Host ""

# 步骤5：测试程序输出
Write-Host "📝 步骤5: 测试程序 Unicode 输出" -ForegroundColor Yellow

# 测试 Python
Write-Host "  Python: " -ForegroundColor Yellow -NoNewline
try {
    $pythonResult = python -c "print('Python Unicode: 你好 🎉')" 2>$null
    if ($LASTEXITCODE -eq 0) {
        Write-Host "✅ PASSED" -ForegroundColor Green
    } else {
        Write-Host "❌ FAILED (Python not found)" -ForegroundColor Red
    }
} catch {
    Write-Host "❌ FAILED (Python not found)" -ForegroundColor Red
}

# 测试 Echo
Write-Host "  Echo: " -ForegroundColor Yellow -NoNewline
try {
    echo "Echo Unicode: 你好 🎉" | Out-Null
    Write-Host "✅ PASSED" -ForegroundColor Green
} catch {
    Write-Host "❌ FAILED" -ForegroundColor Red
}

Write-Host ""

# 步骤6：显示当前设置
Write-Host "📊 当前编码设置:" -ForegroundColor Yellow
Write-Host "  输出编码: $([Console]::OutputEncoding.EncodingName)" -ForegroundColor White
Write-Host "  输入编码: $([Console]::InputEncoding.EncodingName)" -ForegroundColor White
Write-Host "  OEM 代码页: $([Console]::OutputEncoding.CodePage)" -ForegroundColor White

Write-Host ""

# 显示总结和建议
Write-Host "🎉 Unicode 修复完成！" -ForegroundColor Cyan
Write-Host ""
Write-Host "💡 建议:" -ForegroundColor Yellow
Write-Host "1. 如果仍有问题，请重新启动 PowerShell" -ForegroundColor White
Write-Host "2. 推荐使用 Windows Terminal (按 Win+R, 输入 'wt')" -ForegroundColor White
Write-Host "3. 确保字体支持 Unicode (如 Consolas, Cascadia Code)" -ForegroundColor White
Write-Host ""
Write-Host "🧪 再次测试:" -ForegroundColor Cyan
Write-Host "  python -c \"print('✅ 你好!')\"" -ForegroundColor White
Write-Host ""